package cn.pku.sirx.snad.ads.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.pku.sirx.snad.ad.model.AdDescription;
import cn.pku.sirx.snad.ad.model.UserContext;
import cn.pku.sirx.snad.ad.model.entity.AdContent;
import cn.pku.sirx.snad.ad.service.IAdSelectionService;
import cn.pku.sirx.snad.ads.context.UserContextFactory;
import cn.pku.sirx.snad.ads.util.AdDescriptionUtils;
import cn.pku.sirx.snad.ads.util.FormatUtils;

/**
 * Web services of ad.
 * 
 * @author WangXiao
 */
public class AdService {

	/**
	 * Get ad contents as HTML.
	 * 
	 * @param adCount
	 *            count of ads to get
	 * @param contextMap
	 *            a map of user context
	 * @param request
	 *            HttpServletRequest
	 * @return ad contents as HTML
	 */
	public String getAdAsHtml(int adCount, Map<String, String> userContextMap,
			HttpServletRequest request) {
		/*
		 * init user context
		 */
		UserContext userContext = UserContextFactory.getInstance()
				.getUserContext(userContextMap);
		/*
		 * retrieve ads
		 */
		List<AdContent> adContents = adSelectionService.getAds(adCount,
				userContext);
		/*
		 * decorate ads
		 */
		List<AdDescription> adDescriptions = AdDescriptionUtils
				.toAdDescription(adContents, userContext);
		return FormatUtils.formatAds(adDescriptions);
	}

	private IAdSelectionService adSelectionService;

	public void setAdSelectionService(IAdSelectionService adSelectionService) {
		this.adSelectionService = adSelectionService;
	}

}
